package com.generic.dao;

import org.hibernate.criterion.Restrictions;

import com.generic.ds.HibernateSession;
import com.generic.module.Module;

public abstract class ModuleDao implements Dao {

	protected HibernateSession dsSession = null;
	private Class<? extends Module> moduleClass = null;

	public ModuleDao(HibernateSession session, Class<? extends Module> moduleClass) {
		this.dsSession = session;
		this.moduleClass = moduleClass;
	}

	@Override
	public void delete(Module module) {
		dsSession.getSession().delete(module);
	}

	@Override
	public Module find(Long id) {
		return (Module) dsSession.getSession().get(moduleClass, id);
	}

	@Override
	public Module find(String key, String value) {
		return (Module) dsSession.getSession().createCriteria(moduleClass).add(Restrictions.eq(key,value)).uniqueResult();
	}
	
	@Override
	public void insertOrUpdate(Module module) {
		dsSession.getSession().saveOrUpdate(module);
	}
}
